package com.flymobi.mshop.activity;

import java.util.List;
import java.util.Map;
import java.util.Random;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.flymobi.mshop.R;
import com.flymobi.mshop.adapter.ResultAdapter;
import com.flymobi.mshop.util.Constants;
import com.flymobi.mshop.util.service.ProductService;
import com.flymobi.mshop.view.KeywordsFlow;

public class ActKeywordAnim extends CommonActivity implements OnClickListener {
	public static final String[] keywords = { "男鞋", "Sodino", "APK", "GFW",
			"铅笔", "短信", "桌面精灵", "MacBook Pro", "平板电脑", "雅诗兰黛", "卡西欧 TR-100",
			"笔记本", "SPY Mouse", "Thinkpad E40", "捕鱼达人", "内存清理", "地图", "导航",
			"闹钟", "主题", "通讯录", "播放器", "CSDN leak", "安全", "3D", "美女", "天气",
			"4743G", "戴尔", "联想", "欧朋", "浏览器", "愤怒的小鸟", "mmShow", "网易公开课",
			"iciba", "油水关系", "网游App", "互联网", "365日历", "脸部识别", "Chrome",
			"Safari", "中_国版Siri", "A5处理器", "iPhone4S",
			"摩托  ME525″, " + "魅族 M9", "尼康 S2500" };
	private KeywordsFlow keywordsFlow;
	private Button btnIn, btnOut, btnsearch;
	private EditText tvsearch;
	private ProgressDialog mydialog;
	private List<Map<String, Object>> search2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		setContentView(R.layout.searchs);
		btnIn = (Button) findViewById(R.id.btnIn);
		btnOut = (Button) findViewById(R.id.btnOut);
		btnIn.setOnClickListener(this);
		btnOut.setOnClickListener(this);
		keywordsFlow = (KeywordsFlow) findViewById(R.id.keywordsFlow);
		keywordsFlow.setDuration(800l);
		keywordsFlow.setOnItemClickListener(this);
		btnsearch = (Button) findViewById(R.id.btnsearch); // 查找出搜索的按钮
		tvsearch = (EditText) findViewById(R.id.searchText); // 查找出搜索中的文体内容
		btnsearch.setOnClickListener(this);// 搜索商品的事件
		// 添加
		feedKeywordsFlow(keywordsFlow, keywords);
		keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
	}

	private static void feedKeywordsFlow(KeywordsFlow keywordsFlow, String[] arr) {
		Random random = new Random();
		for (int i = 0; i < KeywordsFlow.MAX; i++) {
			int ran = random.nextInt(arr.length);
			String tmp = arr[ran];
			keywordsFlow.feedKeyword(tmp);
		}
	}

	@Override
	public void onClick(View v) {
		if (v == btnIn) {
			keywordsFlow.rubKeywords();
			// keywordsFlow.rubAllViews();
			feedKeywordsFlow(keywordsFlow, keywords);
			keywordsFlow.go2Show(KeywordsFlow.ANIMATION_IN);
		} else if (v == btnOut) {
			keywordsFlow.rubKeywords();
			// keywordsFlow.rubAllViews();
			feedKeywordsFlow(keywordsFlow, keywords);
			keywordsFlow.go2Show(KeywordsFlow.ANIMATION_OUT);
		} else if (v instanceof TextView) {
			String keyword = ((TextView) v).getText().toString();
			select(keyword);
		} else if (v == btnsearch) {
			select(tvsearch.getText().toString());
		}

	}

	private TextView lltishi;

	private void select(final String txt) {
		mydialog = ProgressDialog.show(ActKeywordAnim.this, null, "数据加载中...",
				true);
		mydialog.setCancelable(true);
		new Thread() {
			public void run() {
				try {
					search2 = ProductService.list(txt, Constants.Cls.ad, null);
					System.out.println("文本框=" + tvsearch.getText());
					System.out.println("search2=" + search2);
					if (search2 == null) {
						setContentView(R.layout.tishi);
						lltishi = (TextView) findViewById(R.id.lltishi);

						lltishi.setText("对不起,没有找到与 " + tvsearch.getText()
								+ " 相关的商品");
						lltishi.setTextColor(R.color.heise);
						lltishi.setTextSize(14);
						return;
					}
					Message msg = new Message();
					msg.what = 7;
					hdl.sendMessage(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	private ListView lvresult;
	Handler hdl = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 7) {
				setContentView(R.layout.search_result);
				lvresult = (ListView) findViewById(R.id.lvresult);
				lvresult.setCacheColorHint(R.color.touming);
				ResultAdapter classIfcation = new ResultAdapter(search2,
						ActKeywordAnim.this);
				lvresult.setAdapter(classIfcation);
				mydialog.dismiss();
				return;
			}
		};
	};
}